﻿#include "MeasureView/MeasureView.h"

// #include <QtCore/QResource>
// void initMeasureViewResources()
// {
//     Q_INIT_RESOURCE(measurement);
// }

namespace Qly {

MeasureView::MeasureView(QWidget *parent, int defaultX, int defaultY)
    :ImageView(parent, defaultX, defaultY),
      m_dataset(this)
{
    setMouseTracking(true);
}

MeasureDataSet &MeasureView::dataSet()
{
    return m_dataset;
}

void MeasureView::setPixelSize(double newPixelSize_um, int method)
{
    m_pixelSize_um = newPixelSize_um;
    m_dataset.setPixelSize(newPixelSize_um, method);
    update();
}
void MeasureView::paintEvent(QPaintEvent *event)
{
    ImageView::paintEvent(event);

    QPainter painter(this);
    painter.setRenderHint ( QPainter::Antialiasing, true );
    painter.save();
    painter.scale(m_zoomFactor, m_zoomFactor);
    painter.translate(QPointF(m_size.width() / 2.0, m_size.height() / 2.0));
    m_dataset.paint(painter, m_size.width(), m_size.height());
    painter.restore();
}

QImage MeasureView::image(bool decorate, bool mark)
{
    QImage image = ImageView::image(decorate);

    if(mark)
    {
        QPainter painter(&image);
        painter.setRenderHint ( QPainter::Antialiasing, true );
        int w = image.width();
        int h = image.height();
        painter.translate(QPointF(w / 2.0, h / 2.0));
        m_dataset.paint(painter, w, h);
    }
    return image;
}

bool MeasureView::saveImage(QString fileName, bool decorate)
{
    if(!m_pImage || m_pImage->isNull())
    {
        return false;
    }
    QImage ima = image(decorate, decorate);
    return ima.save(fileName);
}


void MeasureView::mouseMoveEvent(QMouseEvent *event)
{
    ImageView::mouseMoveEvent(event);

    QPointF point_pix = (QPointF)event->pos() / m_zoomFactor;
    point_pix = point_pix - QPointF(m_size.width() / 2.0, m_size.height() / 2.0);

    QPointF point_um = point_pix * m_pixelSize_um;

    if(m_dataset.mouseMove(point_pix, point_um))
    {
        update();
    }
}

void MeasureView::mousePressEvent(QMouseEvent *event)
{
    ImageView::mousePressEvent(event);

    QPointF point_pix = (QPointF)event->pos() / m_zoomFactor;
    point_pix = point_pix - QPointF(m_size.width() / 2.0, m_size.height() / 2.0);
    QPointF point_um = point_pix * m_pixelSize_um;

    m_dataset.m_startPoint_pix = point_pix;
    m_dataset.m_startPoint_um = point_um;
    if( event->button() == Qt::RightButton )
    {
        if(m_dataset.mouseRButtonPress(point_pix, point_um))
        {
            update();
        }
    }
    else if( event->button() == Qt::LeftButton )
    {
        if(m_dataset.mouseLButtonPress(point_pix, point_um))
        {
            update();
        }
    }
}

void MeasureView::mouseReleaseEvent(QMouseEvent *event)
{
    ImageView::mouseReleaseEvent(event);

    QPointF point_pix = (QPointF)event->pos() / m_zoomFactor;
    point_pix = point_pix - QPointF(m_size.width() / 2.0, m_size.height() / 2.0);
    QPointF point_um = point_pix * m_pixelSize_um;

    m_dataset.mouseRelease(point_pix, point_um);
}

}
